//用于存放流程中的审批层数
var personNum = [];
//用于新增流程进行标注
var processTemp = 2;
//用于记录流程列表的最后一个值
var lastProcess = '';
//用于新增审批具体流程标注
var processInfoTemp = 1;
//保存当前点击流程标识
var  currentValue = "";




$(document).ready(function() {
	getProcessList();
});

//查询流程列表
function getProcessList(){
	$.ajax({ 
	 	url: ctp + "/processController/getProcessList.shtml",
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				var html = "";
				if(list != null && list.length > 0){
					for(var i = 0; i < list.length; i++){
						
						if(i == 0){
							html += '<dd class="smart-cell_ltdd active" id="processDD' + list[i].REC_ID + '" onclick="getProcessInfo(\'' + list[i].REC_ID + '\');">'
		                         + '<span id="process' + list[i].REC_ID + '" >' + list[i].NAME + '</span>'
		                         + '<input type="hidden" id="processId' + list[i].REC_ID + '" value="' + list[i].REC_ID + '"/>'
		                         + '</dd>';
							getProcessInfo(list[i].REC_ID);
							currentValue = list[i].REC_ID;
						}else{
							html += '<dd class="smart-cell_ltdd" id="processDD' + list[i].REC_ID + '" onclick="getProcessInfo(\'' + list[i].REC_ID + '\');">'
	                         + '<span id="process' + list[i].REC_ID + '" >' + list[i].NAME + '</span>'
	                         + '<input type="hidden" id="processId' + list[i].REC_ID + '" value="' + list[i].REC_ID + '"/>'
	                         + '</dd>';
						}
						if(i == list.length - 1){
							lastProcess = list[i].REC_ID;
						}
					}
					$("#processList").html(html);
					changeActive();
					
					$("#processId").val($("#processId" + currentValue).val());
					$("#processName").val($("#process" + currentValue).html()=="&nbsp;"?'':$("#process" + currentValue).html());
				}
			}
		}
	});
}

//新增审批流程  + '<i class="layui-icon smart-process_close" onclick="deleteProcess(\'' + processTemp + '\',event);">&#x1006;</i>'
function newProcess(){
	if($("#processId" + lastProcess).val() == ''){
		jalert("有一个新增的审批流程未填写信息或者为保存，请进行相关操作！");
	}else{
		//清空左边流程名称
		$("#processName").val('');
		$("#processId").val('');
		
		var html = '<dd class="smart-cell_ltdd" id="processDD' + processTemp + '" onclick="getProcessInfo(\'' + processTemp + '\');">'
	        + '<span id="process' + processTemp + '" >&nbsp;</span>'
	        + '<input type="hidden" id="processId' + processTemp + '"/>'
	        + '</dd>';
		$("#processList").append(html);
		personNum = [];
		newStartProcess(processInfoTemp, '', '&nbsp;');
		personNum.push(processInfoTemp);
		currentValue = processTemp;
		processInfoTemp++;
		lastProcess = processTemp;
		setLayerNum();
		
		changeActive();
		
	}
}

//查询审批人
function addAuditPerson(temp){
	layui.layer.open({
        type: 2,
        area: ['900px', '475px'],
        title: "添加审核人员",
        fixed: false,
        maxmin: false,
        content: ctp + "/processController/pageProcessPerson.shtml?temp=" + temp
    });
}

//添加审核
function newAuditLayer(){
	if(personNum.length < 5){
		//添加一个新的审核层数
		newProcessInfo(processInfoTemp, '', '&nbsp;');
		//将新成熟的标识放进数组中
		personNum.push(processInfoTemp);
		//将记录成熟的表示加1，方片下一次添加
		processInfoTemp++;
		//设置审批层次
		setLayerNum();
	}else{
		jalert("最多只能添加5级审核！");
		return;
	}
}


//删除流程列表
function deleteProcess(value,event){
	event.stopPropagation();
	if($("#processId" + value).val() != ''){
		jconfirm("del('" + $("#processId" + value).val() + "')", "是否要删除所选记录？");
	}else{
		$("#processDD" + value).remove();
		//清空左边流程列表
		$("#processInfoList").html('');
		$("#processId").val('');
		$("#processName").val('');
	}
}

//删除审批流程环节
function deleteAuditPerson(value){
	jconfirm("delProcessInfo('" + value + "')", "是否要删除所选记录？");
}

function del(value){
	$.ajax({
		url : ctp + "/processController/processDelete.shtml",
		data : {
			processId : value
		},
		type : 'POST',
		async : false,
		success : function(result) {
			if(result == ''){
				jalert9("删除审批流程成功！");
				$("#processDD" + value).remove();
				//清空左边流程列表
				$("#processInfoList").html('');
				$("#processId").val('');
				$("#processName").val('');
			}else{
				jalert(result);
			}
		}
	});
}

function delProcessInfo(value){
	//删除页面显示
	$("#processInfoDIV" + value).remove();
	//删除流程数组里面的标注
	for (var i = 0; i < personNum.length; i++){
		if (personNum[i] == value){
			personNum.splice(i, 1); 
		}
	}
	//重新设置层数
	setLayerNum();
}

function getProcessInfo(value){
	var processId = $("#processId" + value).val();
	if(processId == undefined){
		processId = value;
	}
	currentValue = value;
	$("#processId").val($("#processId" + value).val());
	$("#processName").val($("#process" + value).html()=="&nbsp;"?'':$("#process" + value).html());
	
	$.ajax({ 
	 	url: ctp + "/processController/getProcessInfo.shtml",
	 	data: {"processId":processId},
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				$("#processInfoList").html('');
				personNum = [];
				if(list.length > 0){
					var parentId = '0';
					iterateProcess(list, parentId);
					setLayerNum();
				}else{
					for(var i = 0; i < 3; i ++){
						if(i == 0){
							newFirstProcessInfo(processInfoTemp, '', '');
							personNum.push(processInfoTemp);
							processInfoTemp++;
						}else{
							newProcessInfo(processInfoTemp, '', '');
							personNum.push(processInfoTemp);
							processInfoTemp++;
						}
					}
					setLayerNum();
				}
				
			}
		}
	});
}

//设置层级数
function setLayerNum(){
	for(var i = 0; i < personNum.length; i ++){
		$("#layerNum" + personNum[i]).html(Utils.numberToChinese(i + 1));
	}
}

//循环遍历审批流程
function iterateProcess(list, parentId){
	for(var i = 0; i < list.length; i++){
		if(list[i].PARENT_ID == parentId){
			if(parentId == '0'){
				newFirstProcessInfo(processInfoTemp, list[i].AUDIT_PERSON, list[i].AUDIT_PERSON_NAME);
				personNum.push(processInfoTemp);
				processInfoTemp++;
				iterateProcess(list, list[i].AUDIT_ID);
			}else{
				newProcessInfo(processInfoTemp, list[i].AUDIT_PERSON, list[i].AUDIT_PERSON_NAME);
				personNum.push(processInfoTemp);
				processInfoTemp++;
				iterateProcess(list, list[i].AUDIT_ID);
			}
			
		}
	}
}

function saveProcess(){
	var processName = $("#processName").val();
	var processId = $("#processId").val();
	if(processName=="" || processName=="" ){
		jalert("请输入流程名称！");
		return; 
	}
	
	if(personNum == [] || personNum == null){
		jalert("未设置审批人，不允许添加！");
		return; 
	}
	
	var count = 0;
	var data = '[';
	if(personNum != null){
		for(var i = 0; i < personNum.length; i++){
			count++;
			var auditPerson = $("#auditPerson" + personNum[i]).html();
			var auditRecid = $("#auditRecid" + personNum[i]).val();
			if(auditRecid=="" || auditPerson=="" ){
				jalert("审批人未设置完整！");
				return; 
			}
			data += '{"auditRecid":"' + auditRecid + '","count":"' + count + '"},';
		}
		data = data.substring(0,data.length - 1) + ']';
	}else{
		jalert("请设置审批流程！");
		return; 
	}
	
	
	$.ajax({ 
	 	url: ctp + "/processController/saveProcess.shtml",
	 	data: {"processId":processId,"processName":processName,"data":data,"count":count},
		type : 'POST',
		success : function(result) {
			if(result=="") {
				jalert9("审批流程设置成功！");
			}else{
				if(result.indexOf("-") > 0){
					$("#processId" + processTemp).val(result);
					$("#processId").val(result);
					processTemp++;
					jalert9("审批流程设置成功！");
				}else{
					jalert(result);
				}
				
			}
		}
	});
	
}

//修改流程名，实时回填
function changeProcessName(){
	$("#process" + currentValue).html($("#processName").val());
}


